//==================商家端(B端)======================
const dateKit = require( "./util/dateKit");
const dataKit = require( "./util/dataKit");
const util = require( "./util/util")
const businessService = require( "./util/businessService")

/**
 * 商户登录（保存token）
 */
exports.login = async (context) => {
    const {userNick} = context.data;
    const {openId,accessToken} = context;
    
    if(!userNick){
         return {code:500,msg:"userNick是必须的"};
    }
    if(!accessToken){
         return {code:500,msg:"accessToken是必须的"};
    }

    let merchant = await businessService.getMerchantByOpenId(context,openId);
  
    if(!merchant){
        //首次登陆
        merchant = {
                    openId:openId,
                    createTime:dateKit.getCurrentDateTime()
                  };
    }
    merchant.accessToken = accessToken;
    merchant.loginTime =  dateKit.getCurrentDateTime();
    merchant.userNick = userNick;
    const result = await dataKit.insertOrUpdate(context,"merchant",merchant);
    return {code:200,msg:"登录成功"};
}
